﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IvyBus;
using System.Windows.Media.Media3D;

namespace DisplayInteraction1
{
    class ModelProto2_2Hands
    {
        /*private Ivy bus;
        private Boolean rightHandOpened = true, leftHandDetected = false; 
        private Point3D rightHandPos, leftHandPos;
        private String adresse;

        enum States
        {
            IDLE,
            LIBRE,
            DESSIN,
            EFFACE
        }

        States etat;

        public ModelProto2(String adresse)
        {
            this.adresse = adresse;
            bus = new Ivy("MODELPROTO2", "MODELPROTO2 READY");
            etat = States.LIBRE;
            bus.start(this.adresse);
            bus.bindMsg("^KinectReceiver READY", new Ivy.MessageHandler(viewReady));
        }

        private void reinit()
        {
            bus.stop();
            bus.unBindMsg("^HAND_RIGHT_POSITION X=(.*) Y=(.*) Z=(.*)");
            //bus.unBindMsg("^HAND_LEFT_POSITION X=(.*) Y=(.*) Z=(.*)");
            bus.unBindMsg("^HAND_RIGHT_EVENT=(.*)");
            bus.unBindMsg("^HAND_LEFT_EVENT=(.*)");
            bus.start(this.adresse);
            bus.bindMsg("^KinectReceiver READY", new Ivy.MessageHandler(viewReady));
        }
        
        private void viewReady(IvyClient ic, String[] strings)
        {
            bus.bindMsg("^HAND_RIGHT_POSITION X=(.*) Y=(.*) Z=(.*)", new Ivy.MessageHandler(newRightHandPosition));
            //bus.bindMsg("^HAND_LEFT_POSITION X=(.*) Y=(.*) Z=(.*)", new Ivy.MessageHandler(newLeftHandPosition));
            bus.bindMsg("^HAND_RIGHT_EVENT=(.*)", new Ivy.MessageHandler(rightHandEvent));
            bus.bindMsg("^HAND_LEFT_EVENT=(.*)", new Ivy.MessageHandler(leftHandEvent));
            bus.unBindMsg("^KinectReceiver READY");
        }

        private void send(String s)
        {
            try
            {
                bus.sendMsg(s);
            }
            catch (InvalidOperationException ioe)
            {
                Console.WriteLine("Plus de client pour ModelProto2 : \r\n" + ioe.Message);
                reinit();
            }
        }

        private void newRightHandPosition(IvyClient ic, String[] strings)
        {
            rightHandPos = new Point3D(Double.Parse(strings[0]), double.Parse(strings[1]), double.Parse(strings[2]));
            send("KINECT_POSITION X=" + rightHandPos.X + " Y=" + rightHandPos.Y);
        }

        private void newLeftHandPosition(IvyClient ic, String[] strings)
        {
            leftHandPos = new Point3D(Double.Parse(strings[0]), double.Parse(strings[1]), double.Parse(strings[2]));
        }

        private void rightHandEvent(IvyClient ic, String[] strings)
        {
            String arg = strings[0];
            if (arg.Equals("DETECTED")) {
                switch(etat) {
                    case States.IDLE :
                        if (leftHandDetected) {
                            etat = States.DESSIN;
                            send("KINECT_STATE=DESSIN");
                        } else {
                            etat = States.LIBRE;
                        }
                        break;
                    case States.LIBRE :
                        // Interdit
                        break;
                    case States.DESSIN :
                        // Interdit
                        break;
                    case States.EFFACE :
                        // Interdit
                        break;
                }
            } else if (arg.Equals("LOST")) {
                switch(etat) {
                    case States.IDLE :
                        // Interdit
                        break;
                    case States.LIBRE :
                        etat = States.IDLE;
                        rightHandOpened = true;
                        break;
                    case States.DESSIN :
                        etat = States.IDLE;
                        send("KINECT_STATE=LIBRE");
                        rightHandOpened = true;
                        break;
                    case States.EFFACE :
                        etat = States.IDLE;
                        send("KINECT_STATE=LIBRE");
                        rightHandOpened = true;
                        break;
                }
            } else if (arg.Equals("OPENED")) {
                switch(etat) {
                    case States.IDLE :
                        // Interdit
                        break;
                    case States.LIBRE :
                        rightHandOpened = true;
                        break;
                    case States.DESSIN :
                        // Interdit
                        break;
                    case States.EFFACE :
                        rightHandOpened = true;
                        etat = States.DESSIN;
                        send("KINECT_STATE=DESSIN");
                        break;
                }
            } else if (arg.Equals("CLOSED")) {
                switch(etat) {
                    case States.IDLE :
                        // Interdit
                        break;
                    case States.LIBRE :
                        rightHandOpened = false;
                        break;
                    case States.DESSIN :
                        rightHandOpened = false;
                        etat = States.EFFACE;
                        send("KINECT_STATE=EFFACE");
                        break;
                    case States.EFFACE :
                        // Interdit
                        break;
                }
            } else if (arg.Equals("PUSH"))
            {
                switch (etat)
                {
                    case States.IDLE:
                        // Interdit
                        break;
                    case States.LIBRE:
                        // Interdit
                        break;
                    case States.DESSIN:
                        // Interdit
                        break;
                    case States.EFFACE:
                        send("KINECT_EVENT=EFFACE_ALL");
                        break;
                }
            }
        }

        private void leftHandEvent(IvyClient ic, String[] strings)
        {
            String arg = strings[0];
            if (arg.Equals("DETECTED")) {
                switch(etat) {
                    case States.IDLE :
                        leftHandDetected = true;
                        break;
                    case States.LIBRE :
                        leftHandDetected = true;
                        if (rightHandOpened) {
                            etat = States.DESSIN;
                            send("KINECT_STATE=DESSIN");
                        } else {
                            etat = States.EFFACE;
                            send("KINECT_STATE=EFFACE");
                        }
                        break;
                    case States.DESSIN :
                        // Interdit
                        break;
                    case States.EFFACE :
                        // Interdit
                        break;
                }
            } else if (arg.Equals("LOST")) {
                switch(etat) {
                    case States.IDLE :
                        leftHandDetected = false;
                        break;
                    case States.LIBRE :
                        leftHandDetected = false;
                        break;
                    case States.DESSIN :
                        leftHandDetected = false;
                        etat = States.LIBRE;
                        send("KINECT_STATE=LIBRE");
                        break;
                    case States.EFFACE :
                        leftHandDetected = false;
                        etat = States.LIBRE;
                        send("KINECT_STATE=LIBRE");
                        break;
                }
            } else if (arg.Equals("OPENED")) {
                
            } else if (arg.Equals("CLOSED")) {
                
            }*/
        //}
    }
}
